package com.threaddemo4;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

public class HandleDemo {
    public static void main(String[] args)  {
        CompletableFuture<String> f1 = CompletableFuture.supplyAsync(()->{
            System.out.println("F1 " + Thread.currentThread().getName());
            int i = 10 / 0;
            return "F1 返回值";
        });
        //param:上一个任务执行返回结果
        //e:上一个任务执行抛出的异常 如果没有异常 则为null
        CompletableFuture handle = f1.handle((param,e)->{
            if(e!=null){
                System.out.println("异常信息 = " + e.getMessage());
            }else{
                System.out.println("参数 = " + param);
            }
            return "Handle的返回值....";
        });

        try{
            System.out.println("f1 = " + f1.get());
        }catch (Exception e){
            System.out.println("F1 任务有异常 = " + e.getMessage());
        }
        try{
            System.out.println("handle = " + handle.get());
        }catch (Exception e){
            System.out.println("handle 异常 = " + e.getMessage());
        }

    }
}
